package com.example.qinjiasheng.broadcasttest;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private IntentFilter mIntentFilter;
    private LocalReceiver mLocalReceiver;
    private LocalBroadcastManager mLocalBroadcastManager;
//    private NetWorkChangeReceiver mNetWorkChangeReceiver;
    private Button mSendButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mIntentFilter = new IntentFilter();
//        mIntentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        mNetWorkChangeReceiver = new NetWorkChangeReceiver();
//        registerReceiver(mNetWorkChangeReceiver, mIntentFilter);
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
        mSendButton = (Button) findViewById(R.id.send_broadcast);
        mSendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Intent intent = new Intent("com.example.qinjiasheng.broadcasttest.MY_BROADCAST");
                Intent intent = new Intent("com.example.qinjiasheng.broadcasttest.LOCAL_BROADCAST");
                mLocalBroadcastManager.sendBroadcast(intent);
//                sendBroadcast(intent);
//                sendOrderedBroadcast(intent, null);
            }
        });
        mIntentFilter.addAction("com.example.qinjiasheng.broadcasttest.LOCAL_BROADCAST");
        mLocalReceiver = new LocalReceiver();
        mLocalBroadcastManager.registerReceiver(mLocalReceiver,mIntentFilter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        unregisterReceiver(mNetWorkChangeReceiver);
        unregisterReceiver(mLocalReceiver);
    }

    private class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
                Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
        }
    }
//    private class NetWorkChangeReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
//            if (networkInfo != null && networkInfo.isAvailable()) {
//                Toast.makeText(context, "network is available", Toast.LENGTH_SHORT).show();
//            } else {
//                Toast.makeText(context, "network is unavailable", Toast.LENGTH_SHORT).show();
//            }
//        }
//    }
}
